<?php

namespace app\business\controller;

use think\Controller;
use Util\SysDb;
use think\Db;
class Login extends Controller
{
    public function login()
    {
        
        $username = trim(input('param.account'));
        $password = input('param.password');

        if ($username == '') {
            exit(json_encode(['code' => 1, 'msg' => 'error']));
        } elseif ($password == '') {
            exit(json_encode(['code' => 1,'message' => 'error', ]));
        }

        // 验证用户
        $this->db = new SysDb();
        $admin = $this->db->table('bus_info')->where(['phone' => $username])->item();
        if (!$admin) {
            exit(json_encode(['code' => 1, 'message' => 'error',"result"=>"1" ]));
        }
        if (md5($password) != $admin['password']) {
            exit(json_encode(['code' => 1, 'message' => 'error',"result"=>"2"]));
        }
        if ($admin['status'] == 1) {
            exit(json_encode(['code' => 1, 'message' => 'error',"result"=>"3"]));
        }
        if ( $admin['status'] == 0) {
            exit(json_encode(['message' =>'succ','result'=>$admin]));
        }
        // if ($admin['status'] == 0) {
        //     exit(json_encode(['message' => 'succ']));
        // }
        
    }
    public function register() 
    {
        
        $phone = trim(input('param.phone'));
        $password = input('param.password');
        $qq = input('param.qq');
        $psd = md5($password);
        $data['phone'] = trim(input('param.phone'));
        $data['password'] = $psd;
        $data['status'] = 0;
        $data['create_time'] = date('Y-m-d h:i:s', time());
        $data['qq'] = $qq;
        $data['email'] = "";
        $data['tenant_id'] = 1;
        $data['withdraw_status'] = 2;
        $data['balance'] = 0;
        $data['total_amount'] = 0;
        $data['level_id'] = 1;
        $data['parent_id'] = 0;
        $data['invite_count'] = 0;
        $data['remark'] = "";
        $data['openid'] = "";
        $data['first_recharge_status'] = 0;
        $data['first_recharge_time'] = date('Y-m-d h:i:s', time());
        if ($phone == '') {
            exit(json_encode(['result' => "手机号不能为空", 'message' => 'error']));
        } elseif ($password == '') {
            exit(json_encode(['result' => "密码不能为空",'message' => 'error', ]));
        }

        // 验证用户
        $this->db = new SysDb();
        $admin = $this->db->table('bus_info')->where(['phone' => $phone])->item();
        if($admin){
            exit(json_encode(['result' => "手机号已存在，请重新输入",'message' => 'error', ]));
        }
        $id = $this->db->table('bus_info')->count();
        $data['id'] = $id+1;
        $this->db->table('bus_info')->insert($data);
        $summany= DB::name("sys_summary")->order("id desc")->find();
            $idw=$summany['id'];
            $arraym=explode(" ",$summany['create_time']);
            if($arraym[0]==date("Y-m-d")){
                $summany['bus_num']=$summany['bus_num']+1;
                $this->db->table('sys_summary')->where(['id' => $idw])->update($summany);
            }
            else{
                $summany['tenant_id']=trim(input('param.tenantId'));
                $summany['bus_num']=1;
                $summany['recharge']=0;
                $summany['order_num']=0;
                $summany['pk_num']=0;
                $summany['profit']=0;
                $summany['tenant_profit']=0;
                $this->db->table('sys_summary')->insert($summany);
            }
        exit(json_encode(['code' => 0, 'message' => 'succ']));
    }
    public function info()
    {
        
        $username = trim(input('param.phone'));
        if ($username == '') {
            exit(json_encode(['code' => 1, 'msg' => 'error',"result"=>"无信息"]));
        }
        $this->db = new SysDb();
        $admin = $this->db->table('bus_info')->where(['phone' => $username])->item();
        exit(json_encode(['message' =>'succ','result'=>$admin]));
      
        
    }
    public function tenantinfo()
    {
        $username = trim(input('param.locations'));
        if ($username == '') {
            exit(json_encode(['code' => 1, 'msg' => 'error',"result"=>"无信息"]));
        }
        $this->db = new SysDb();
        $admin = $this->db->table('tenant_info')->where(['domain_name' => $username])->item();
        exit(json_encode(['message' =>'succ','result'=>$admin]));
      
        
    }

    public function showParams()
    {
        $this->db = new SysDb();
        $admin = $this->db->table('sys_params')->lists();
        $data['balance_openid']=$admin[0]["value"];
        $data['balance_price']=$admin[1]["value"];
        $data['no_show_other_site']=$admin[2]["value"];
        $data['no_show_me_ad']=$admin[3]["value"];
        exit(json_encode(['message' =>'succ','result'=>$data]));
      
        
    }
    public function updatePassword(){
        $id = trim(input('param.id'));
        $password = trim(input('param.password'));
        $this->db = new SysDb();
        $admin = $this->db->table('bus_info')->where(['id' => $id])->item();
        $psd = md5($password);
        $admin['password']=$psd;
        $this->db->table('bus_info')->where(['id' =>$id])->update($admin);
        exit(json_encode(['message' =>'succ','result'=>"修改成功"]));

    }
    public function update(){
        $id = trim(input('param.id'));
        $phone=trim(input('param.phone'));
        $wechat=trim(input('param.wechat'));
        $qq=trim(input('param.qq'));
        $email=trim(input('param.email'));
        $this->db = new SysDb();
        $admin = $this->db->table('bus_info')->where(['id' => $id])->item();
        $admin['phone']=$phone;
        $admin['wechat']=$wechat;
        $admin['email']=$email;
        $admin['phone']=$phone;
        $admin['qq']=$qq;
        $this->db->table('bus_info')->where(['id' =>$id])->update($admin);
        exit(json_encode(['message' =>'succ','result'=>"修改成功"]));

    }
    
}

